﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;

namespace WinSPen.BaseClass
{
    public static class Util
    {
        /// <summary>
        /// 콘솔 로그용 함수.
        /// </summary>
        /// <param name="caller">호출한 클래스 인스턴스</param>
        /// <param name="obj">메시지를 출력할 대상 object</param>
        public static void Log(object caller, object obj)
        {
            string message = "";
            message += "[" + TypeDescriptor.GetClassName(caller) + "] ";
            StackFrame callStack = new StackFrame(1, true);
            message += "<" + callStack.GetMethod().Name + ">";
            if (obj != null)
            {
                if (obj.GetType() == message.GetType())
                {
                    message += obj as string;
                }
                else
                {
                    message += "Instance of Class : " + TypeDescriptor.GetClassName(obj) + "\n";
                    PropertyDescriptorCollection propNames = TypeDescriptor.GetProperties(obj, true);
                    foreach (PropertyDescriptor prop in propNames)
                    {

                        message += "\t - " + prop.Name + "\t : " + prop.GetValue(obj) + "\n";
                    }
                }
            }
            System.Diagnostics.Trace.WriteLine(message);
        }
        /// <summary>
        /// Exception 발생용 함수.
        /// </summary>
        /// <param name="msg">exception에 포함시킬 내용</param>
        public static void ThrowException(string msg)
        {
            StackFrame callStack = new StackFrame(1, true);
            string expString = "[User Exception] " + msg + "\n File : " + callStack.GetFileName() + " / Line : " + callStack.GetFileLineNumber();
            throw new Exception(expString);
        }
    }


}
